# Player.gd - 主角角色控制脚本
# 《光影之间》- 无名之子角色控制器

extends CharacterBody2D
class_name Player

# 移动参数
@export var speed := 300.0
@export var jump_velocity := -600.0
@export var gravity := 980.0

# 影视能力参数
@export var shadow_vision_energy := 100.0
@export var shadow_vision_max_energy := 100.0
@export var shadow_vision_drain_rate := 20.0
@export var shadow_vision_recovery_rate := 10.0

# 状态变量
var is_shadow_vision_active := false
var can_move := true
var facing_direction := 1  # 1为右，-1为左

# 节点引用
@onready var sprite := $Sprite2D
@onready var collision_shape := $CollisionShape2D
@onready var shadow_vision_area := $ShadowVisionArea
@onready var camera := $Camera2D
@onready var shadow_vision_effect := $ShadowVisionEffect

# 信号
signal shadow_vision_toggled(is_active: bool)
signal energy_changed(current_energy: float, max_energy: float)
signal player_died()
signal player_damaged(damage: int)
signal item_collected(item_type: String)
signal player_state_changed(new_state: String)
signal player_moved
signal player_jumped

func _ready():
	# 初始化影视效果
	shadow_vision_effect.visible = false
	shadow_vision_effect.color = Color(0.2, 0.2, 0.8, 0.3)
	
	# 连接信号
	shadow_vision_toggled.connect(_on_shadow_vision_toggled)
	
	print("主角已准备就绪 - 无名之子觉醒")

func _physics_process(delta):
	# 处理重力
	if not is_on_floor():
		velocity.y += gravity * delta
	
	# 处理移动输入
	if can_move:
		handle_movement_input()
		handle_shadow_vision_input()
	
	# 更新影视能量
	update_shadow_vision_energy(delta)
	
	# 移动角色
	move_and_slide()
	
	# 更新精灵朝向
	update_sprite_direction()

func handle_movement_input():
	# 水平移动
	var direction = Input.get_axis("move_left", "move_right")
	if direction != 0:
		velocity.x = direction * speed
		facing_direction = sign(direction)
		player_moved.emit()  # 发送移动信号
	else:
		velocity.x = move_toward(velocity.x, 0, speed * 0.1)
	
	# 跳跃
	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = jump_velocity
		player_jumped.emit()  # 发送跳跃信号
		print("主角跳跃")

func handle_shadow_vision_input():
	# 切换影视能力
	if Input.is_action_just_pressed("shadow_vision"):
		toggle_shadow_vision()

func toggle_shadow_vision():
	if shadow_vision_energy > 0:
		is_shadow_vision_active = !is_shadow_vision_active
		shadow_vision_toggled.emit(is_shadow_vision_active)
		print("影视能力状态: ", "激活" if is_shadow_vision_active else "关闭")

func update_shadow_vision_energy(delta):
	if is_shadow_vision_active:
		# 消耗能量
		shadow_vision_energy -= shadow_vision_drain_rate * delta
		if shadow_vision_energy <= 0:
			shadow_vision_energy = 0
			is_shadow_vision_active = false
			shadow_vision_toggled.emit(false)
			print("影视能量耗尽")
	else:
		# 恢复能量
		shadow_vision_energy += shadow_vision_recovery_rate * delta
		shadow_vision_energy = min(shadow_vision_energy, shadow_vision_max_energy)
	
	# 发送能量变化信号
	energy_changed.emit(shadow_vision_energy, shadow_vision_max_energy)

func update_sprite_direction():
	# 更新精灵朝向
	if facing_direction > 0:
		sprite.scale.x = abs(sprite.scale.x)
	else:
		sprite.scale.x = -abs(sprite.scale.x)

func _on_shadow_vision_toggled(is_active: bool):
	# 更新影视效果显示
	shadow_vision_effect.visible = is_active
	
	# 通知游戏世界影视状态变化
	get_tree().call_group("shadow_vision_objects", "set_shadow_vision_active", is_active)

# 受到伤害
func take_damage(damage: int):
	print("主角受到伤害: ", damage)
	# 这里可以添加受伤动画和效果
	
# 死亡处理
func die():
	can_move = false
	print("主角死亡")
	player_died.emit()

# 收集道具
func collect_item(item_type: String, value: int = 1):
	print("收集道具: ", item_type, " 数量: ", value)
	
	match item_type:
		"light_crystal":
			# 恢复影视能力能量
			shadow_vision_energy = min(shadow_vision_energy + 20.0, shadow_vision_max_energy)
			print("光晶石收集，影视能量恢复")
		"health_potion":
			# 恢复生命值（如果有生命系统）
			print("生命药水收集")
		"key":
			# 收集钥匙
			print("钥匙收集")
	
	# 发送收集信号
	item_collected.emit(item_type)

# 设置玩家位置
func set_player_position(pos: Vector2):
	global_position = pos

# 设置玩家状态
func set_player_state(state: Dictionary):
	if state.has("position"):
		global_position = state.position
	if state.has("shadow_vision_energy"):
		shadow_vision_energy = state.shadow_vision_energy
	if state.has("can_move"):
		can_move = state.can_move

# 获取玩家状态信息
func get_player_state() -> Dictionary:
	return {
		"position": global_position,
		"shadow_vision_energy": shadow_vision_energy,
		"is_shadow_vision_active": is_shadow_vision_active,
		"facing_direction": facing_direction,
		"can_move": can_move
	}

# 重置玩家状态
func reset_player_state():
	shadow_vision_energy = shadow_vision_max_energy
	is_shadow_vision_active = false
	can_move = true
	velocity = Vector2.ZERO
	facing_direction = 1